package com.heatingcharge.service.charge.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.util.ChargeStateEnum;
import com.heatingcharge.dto.ChargeInfoDTO;
import com.heatingcharge.dto.ChargeStatisticInfoDTO;
import com.heatingcharge.dto.CurrentDayChargeInfo;
import com.heatingcharge.entity.charge.ChargeInfo;
import com.heatingcharge.entity.charge.ChargePayable;
import com.heatingcharge.mapper.charge.ChargeInfoMapper;
import com.heatingcharge.mapper.charge.ChargePayableMapper;
import com.heatingcharge.param.ChargeInfoParam;
import com.heatingcharge.param.SearchParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.charge.IChargeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-10-17
 */
@Service
public class ChargeInfoServiceImpl extends ServiceImpl<ChargeInfoMapper, ChargeInfo> implements IChargeInfoService {
    @Autowired
    ChargePayableMapper chargePayableMapper;

    @Override
    public IPage<ChargeInfoDTO> getChargeInfo(Page page, SearchParam searchParam) {
        return baseMapper.selectChargeInfo(page,searchParam);
    }

    @Override
    public List<ChargeInfoDTO> getChargeInfo(SearchParam searchParam) {
        return baseMapper.selectChargeInfo(searchParam);
    }
    @Override
    public List<ChargeInfoDTO> getHouseChargeInfo(SearchParam searchParam) {
        List<ChargeInfoDTO> chargeInfoDTOS = baseMapper.selectHouseChargeInfo(searchParam);
        return baseMapper.selectHouseChargeInfo(searchParam);
    }

    @Override
    public Integer charge(ChargeInfoParam chargeInfoParam) {
        JwtUser user = SecurityUtil.getUser();
        return charge(chargeInfoParam.getChargePayableId(),
                chargeInfoParam.getActualPayable(),
                chargeInfoParam.getActual(),
                chargeInfoParam.getAdditional(),
                chargeInfoParam.getReduction(),
                chargeInfoParam.getSubsidy(),
                chargeInfoParam.getDiscount(),
                chargeInfoParam.getLateFee(),
                1,
                chargeInfoParam.getChargeDate(),
                user.getRealName());
    }

    @Override
    public Integer charge(Integer chargePayableId, BigDecimal actualPayable, BigDecimal actual,
                          BigDecimal additional, BigDecimal reduction, BigDecimal subsidy,
                          BigDecimal discount,BigDecimal lateFee, Integer chargeModeId, Date chargeDate,String payee) {
//        JwtUser user= SecurityUtil.getUser();
        QueryWrapper<ChargePayable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",chargePayableId);
        queryWrapper.eq("del_flag",0);
        ChargePayable chargePayable = chargePayableMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotNull(chargePayable)){
            Integer chargeState=2;//欠费
            //附加费用
            chargePayable.setAdditional(additional);
            //减免金额
            chargePayable.setReduction(reduction);
            //补贴金额
            chargePayable.setSubsidy(subsidy);
            //优惠金额
            chargePayable.setDiscount(discount);
             //滞纳金
            chargePayable.setLateFee(lateFee);
            //实际应缴
            chargePayable.setActualPayable(actualPayable);
            //实际缴费
//            chargePayable.setAc(actualPayable);
            if(actualPayable.equals(actual)){
                chargeState=1;
                chargePayable.setChargeState(chargeState);
            }
            chargePayableMapper.updateById(chargePayable);
            ChargeInfo chargeInfo=ChargeInfo.builder()
                    .houseId(chargePayable.getHouseId())
                    .heatYearId(chargePayable.getHeatYearId())
                    .actual(actual)
                    .chargeState(chargeState)
                    .chargeModeId(chargeModeId)
                    .chargeDate(chargeDate)
                    .payee(payee)
                    .remark("")
                    .build();
            return baseMapper.insert(chargeInfo);
        }
        return null;
    }

    @Override
    public Integer add(Integer chargePayableId,BigDecimal actual,Integer chargeModeId) {
        JwtUser user = SecurityUtil.getUser();
        ChargePayable chargePayable = chargePayableMapper.selectById(chargePayableId);
        if (ObjectUtil.isNotNull(chargePayable)){
            Integer chargeState=0;
            Integer compare=chargePayable.getActualPayable().compareTo(actual);
            if (compare==0){
                chargeState= ChargeStateEnum.PAYOFF.getCode();
            }
            else if (compare==1){
                chargeState= ChargeStateEnum.ARREARS.getCode();
            }
            ChargeInfo chargeInfo=ChargeInfo.builder()
                    .houseId(chargePayable.getHouseId())
                    .heatYearId(chargePayable.getHeatYearId())
                    .actual(actual)
                    .chargeState(chargeState)
                    .chargeModeId(chargeModeId)
                    .chargeDate(new Date())
                    .payee(user.getRealName())
                    .remark(user.getRealName())
                    .build();
            return baseMapper.insert(chargeInfo);
        }
        return null;
    }

    @Override
    public CurrentDayChargeInfo getCurrentDayInfo() {
        return baseMapper.selectCurrentDayInfo();
    }

    @Override
    public ChargeStatisticInfoDTO getChargeMode() {
        return baseMapper.selectChargeMode();
    }
}
